import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/pages/my/controller/presale_order_ctr.dart';
import 'package:cbec_app/pages/my/view/presale_order_Item.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../../../components/popup/after_sales_popup.dart';
import '../../../routes/routes.dart';

///预售订单
class PreSaleOrder extends GetView<PresaleOrderController> {
  const PreSaleOrder({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PresaleOrderController>(builder: (controller) {
      return BasePage(
        title: "我的预售",
        iconType: IconType.back,
        isShowPadding: true,
        tabHeight: 100.w,
        tabBgColor: Tools.fromHex("#F3F5F7"),
        tabChildren: [
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: _search(),
          ),
          _topTabbar()
        ],
        refreshController: controller.baseRefreshCtr,
        enablePullDown: true,
        enablePullUp: controller.hasNextPage,
        onPageLoading: controller.onPageLoading,
        onPageRefresh: controller.onPageRefresh,
        children: [
          Container(
            child: controller.orderList.isNotEmpty
                ? ListView(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    //这里是订单
                    children: List.generate(controller.orderList.length, (index) {
                      var item = controller.orderList[index];
                      // print('status === ${item['status']}');
                      print('status === ${item}');
                      return ListView.builder(
                          itemCount: controller.orderList.length,
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemBuilder: (context, index) {
                            var item = controller.orderList[index];
                            return PresaleOrderItem(
                              itemInfo: item,
                              detailOnTap: () {
                                Get.toNamed(Routes.pendingPayment, arguments: item['id']);
                              },
                              onCallback: (type, itemInfo) {
                                int commodityType = item['commodityType'];
                                bool hasBonded = false;
                                if (commodityType == 1) {
                                  hasBonded = true;
                                }
                                switch (type) {
                                  case OperateActionType.goOnPayment:
                                    // 继续支付

                                    Get.toNamed(
                                      '${Routes.cashier}/?sn=${itemInfo['paySn']}&price=${itemInfo['priceOrder']}&cd=${itemInfo['cd']}',
                                      arguments: {'hasBonded': hasBonded},
                                    );

                                  case OperateActionType.depositPayment:
                                    // 支付定金
                                    Get.toNamed(
                                      '${Routes.cashier}/?sn=${itemInfo['paySn']}&price=${itemInfo['priceOrder']}&cd=${itemInfo['cd']}&hasBonded=$hasBonded',
                                      arguments: {'hasBonded': hasBonded},
                                    );
                                  case OperateActionType.finalPayment:
                                    // 支付尾款
                                    if (Tools.isPresaleEnded(itemInfo['presaleEndTime'])) {
                                      Tools.showToast('支付尾款时间结束');
                                      return;
                                    }
                                    Get.toNamed(Routes.pendingPayment, arguments: item['id']);
                                  case OperateActionType.cancelOrder:
                                    // 取消订单
                                    controller.cancelOrder(itemInfo['id']);
                                  case OperateActionType.confirmOrder:
                                    // 确认收货
                                    Tools.showModal(
                                      content: "确认要收货么?",
                                      onCancel: (tag) => {},
                                      onConfirm: (tag) {
                                        controller.receive(itemInfo['id']);
                                        SmartDialog.dismiss(tag: tag);
                                      },
                                    );

                                  case OperateActionType.requestReturn:
                                    afterSalesPopup(context, itemInfo['goodsInfo'], itemInfo['id'], afterSaleStyle: 2);
                                  default:
                                    break;
                                }
                              },
                            );

                            // return OrderItem(
                            //   orderSn: item['id'],
                            //   orderTime: item['cd'],
                            //   totalAmount: item['priceOrder'],
                            //   status: item['status'],
                            //   goodsList: item['goodsInfo'],
                            //   id: item['id'],
                            //   sn: item['sn'],
                            //   paySn: item['paySn'],
                            //   isPresale: true,
                            //   priceOrder: item['priceOrder'],
                            //   presaleData: item,
                            // );
                          }
                          // )
                          );
                    }))
                : const NotFound(),
          )
        ],
      );
    });
  }

  //上方搜索框
  Widget _search() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Tools.fromHex("#D7D7D7"),
          width: 1.0,
        ),
        borderRadius: BorderRadius.circular(50.0),
      ),
      height: 30.h,
      margin: EdgeInsets.only(top: 10.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 5.w,
          ),
          Icon(
            Icons.search,
            color: Tools.fromHex("D7D7D7"),
          ),
          SizedBox(
            width: 5.w,
          ),
          SizedBox(
            width: 200.w,
            child: TextField(
              controller: controller.searchTxt,
              onSubmitted: (value) {
                controller.search(value);
              },
              decoration: descript(
                "搜索我的订单",
                fontsize: 15,
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _topTabbar() => Container(
        margin: EdgeInsets.only(top: 10.w),
        height: 25,
        child: TabBar(
            dividerColor: Colors.transparent,
            indicator: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.8, color: Tools.fromHex("#F83F2D")),
              ),
            ),
            labelColor: Colors.red,
            labelStyle: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.black),
            unselectedLabelColor: Colors.black,
            // isScrollable: true,
            controller: controller.tabController,
            tabAlignment: TabAlignment.fill,
            labelPadding: const EdgeInsets.symmetric(horizontal: 5),
            tabs: const [
              Tab(
                text: "全部",
              ),
              Tab(
                text: "待支付",
              ),
              Tab(
                text: "待发货",
              ),
              Tab(
                text: "待收货",
              ),
              Tab(
                text: "已取消",
              ),
              Tab(
                text: "已完成",
              ),
            ]),
      );
}
